﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using CSKLiteWindowsPhone.WebsiteData;
using System.IO.IsolatedStorage;
using CSKLiteWindowsPhone.Common;
using Coding4Fun.Phone.Controls;

namespace CSKLiteWindowsPhone
{
    public partial class Login : PhoneApplicationPage
    {
        private WebsiteDataSoapClient _websiteDataClient;
        public Login()
        {
            InitializeComponent();

            this.Loaded += new RoutedEventHandler(Login_Loaded);
        }

        void Login_Loaded(object sender, RoutedEventArgs e)
        {
        }

        void _websiteDataClient_LoginShortCompleted(object sender, LoginShortCompletedEventArgs e)
        {
            if (e.Error == null)
            {
                LoggedIn(e.Result);
            }
            else
            {
                //todo
            }

            this.ApplicationBar.IsVisible = true;
            progressOverlay.Hide();
            
        }

        

        private void btnLogin_Click(object sender, EventArgs e)
        {

            //string userName = "fred.hays@websense.com";
            //string password = "password";


            this.ApplicationBar.IsVisible = false;
            progressOverlay.Show();
            string userName = this.txtUserName.Text.Trim();
            string password = this.txtPassword.Password.Trim();
            this._websiteDataClient = Utility.GetWebsiteDataSoapClient();
            this._websiteDataClient.LoginAsync(userName, password);
            this._websiteDataClient.LoginCompleted += new EventHandler<LoginCompletedEventArgs>(_websiteDataClient_LoginCompleted);
        }

        void _websiteDataClient_LoginCompleted(object sender, LoginCompletedEventArgs e)
        {
            if (e.Error == null)
            {
                LoggedIn(e.Result);
            }
            else
            {
                ToastPrompt toast = new ToastPrompt();
                string error = e.Error.Message;
                toast.Message = error;
                toast.TextWrapping = TextWrapping.Wrap;
                toast.Show();
            }

            this.ApplicationBar.IsVisible = true;
            progressOverlay.Hide();
        }

        void LoggedIn(LoginResponse loginResponse)
        {
            if (loginResponse.Errors.Count() > 0)
            {
                ToastPrompt toast = new ToastPrompt();
                string error = string.Empty;
                Array.ForEach(loginResponse.Errors, err =>
                {
                    error = err.Message + ";";
                });
                toast.Message = error;
                toast.TextWrapping = TextWrapping.Wrap;
                toast.Show();
            }
            else
            {
                string token = loginResponse.Token;
                IsolatedStorageSettings.ApplicationSettings.Remove(Constants.LoginTockenKey);
                IsolatedStorageSettings.ApplicationSettings.Add(Constants.LoginTockenKey, token);

                IsolatedStorageSettings.ApplicationSettings.Remove(Constants.LoginUserNameKey);
                IsolatedStorageSettings.ApplicationSettings.Add(Constants.LoginUserNameKey, this.txtUserName.Text.Trim());

                NavigationService.Navigate(new Uri("/MainPage.xaml", UriKind.Relative));
            }
        }

        private void btnSettings_Click(object sender, EventArgs e)
        {
            NavigationService.Navigate(new Uri("/Settings.xaml", UriKind.Relative));
        }
    }
}